//author:kakake
//1.预约挂号支付，调起收银台支付，支付成功后会回调通知tspaynotifyController
//2.预约挂号退费，调起退费接口，退款成功后会回调通知tspaynotifyController
//3.自助缴费支付，调起收银台支付，支付成功后会回调通知tspaynotifyController

//author:kakake
var Common = /** @class */ (function () {
    function Common() {
    }
    return Common;
}());

//author:kakake
var tsselfpayDao = /** @class */ (function () {
    function tsselfpayDao(workid) {
        this.data = {
            dbConn: TSLobsterAPI.Program.GetDbConn(),
            WorkId: 1
        };
        this.data.WorkId = workid;
    }
    tsselfpayDao.prototype.getPresPaymentInfo = function (id) {
        var strsql = "select * from WPMT_PresPayment where Id=@id";
        return TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { id: id });
    };
    //更新已支付状态
    tsselfpayDao.prototype.updatePresPayed = function (Id, TranSerNo, OrderNo, PayMode) {
        var strsql = " update WPMT_PresPayment set PayStatus=2,OrderState=2,PayTime=@PayTime,PayMode=@PayMode,OrderNo=@OrderNo,TranSerNo=@TranSerNo where Id=@Id";
        var dbparams = {
            Id: Id,
            TranSerNo: TranSerNo,
            OrderNo: OrderNo,
            PayTime: TSLobsterAPI.Utils.getCurrTime(null),
            PayMode: PayMode
        };
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
    };
    return tsselfpayDao;
}());

//author:kakake
var tsselfpayService = /** @class */ (function () {
    function tsselfpayService(workid) {
        this.data = {
            dbConn: TSLobsterAPI.Program.GetDbConn(),
            WorkId: 1
        };
        this.PresPayment = {
            "model": {
                "Id": 0,
                "CardNum": "",
                "CardType": 0,
                "PatName": "",
                "Regno": "",
                "Repnos": "",
                "Fee": "",
                "PayStatus": 0,
                "CreateTime": "yyyy-MM-dd",
                "UpdateTime": "yyyy-MM-dd",
                "OrderNo": "",
                "UniqueNumber": "",
                "TranserNo": "",
                "PayMode": 0,
                "OrderState": 0,
                "PayTime": "",
                "WorkId": 0,
                "MpUserId": "",
                "BranchHospitalId": 0
            },
            "tablename": "WPMT_PresPayment",
            "key": "Id",
            "val": "0",
            "objval": {},
            "query": {},
            "where": "",
            "pageinfo": {},
            "WorkId": 1
        };
        this.FeeDetail = {
            "model": {
                "FeeDetailId": 0,
                "RecordId": 0,
                "PatListId": "",
                "Repno": "",
                "PresDoctorID": "",
                "PresDoctName": "",
                "PresDeptID": 0,
                "PresDeptName": "",
                "ExecDeptId": "",
                "ExecDeptName": "",
                "FeeTime": "yyyy-MM-dd",
                "ItemName": "",
                "FeeTypeName": 0,
                "UnitPrice": "",
                "Quantity": "",
                "Specs": "",
                "Usage": "",
                "Frequency": "",
                "Unit": "",
                "CreateTime": "yyyy-MM-dd",
                "UpdateTime": "yyyy-MM-dd",
                "WorkId": 0
            },
            "tablename": "WPMT_FeeDetail",
            "key": "FeeDetailId",
            "val": "0",
            "objval": {},
            "query": {},
            "where": "",
            "pageinfo": {},
            "WorkId": 1
        };
        this.data.WorkId = workid;
        this.hisinterface = new tshisinterfaceController(1);
        this.dao = new tsselfpayDao(this.data.WorkId);
    }
    //缴费处方
    tsselfpayService.prototype.balance = function (model) {
        var _this = this;
        this.PresPayment.WorkId = this.data.WorkId;
        this.FeeDetail.WorkId = this.data.WorkId;
        // var model = JSON.parse(TSLobsterAPI.Program.RequestBody());
        try {
            this.data.dbConn.BeginTransaction();
            model.Repnos = model.Repnos.join(",");
            model.PayStatus = 1; //1 未支付 2已支付
            model.CreateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
            model.UpdateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
            model.OrderNo = ""; //从支付平台获取
            model.UniqueNumber = "";
            model.PayMode = 1; //微信
            model.OrderState = 1; //订单状态: 1待付款、2已付款、5已取消、6退款中、7退款完成、
            model.PayTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
            model.MpUserId = TSLobsterAPI.Program.GetMPUserId();
            this.PresPayment.objval = model;
            TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.PresPayment);
            model.Id = this.PresPayment.objval['Id'];
            model.FeeItems.forEach(function (item) {
                item.RecordId = _this.PresPayment.objval['Id'];
                item.CreateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
                item.UpdateTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
                _this.FeeDetail.objval = item;
                TSLobsterAPI.DbHelper.SaveModel(_this.data.dbConn, _this.FeeDetail);
            });
            this.data.dbConn.CommitTransaction();
        }
        catch (e) {
            this.data.dbConn.RollbackTransaction();
            throw e;
        }
        //测试支付成功后调用his接口
        //this.balancepaysuccess(model.Id);
        var workid = TSLobsterAPI.Program.GetWorkId();
        var appid = TSLobsterAPI.Program.GetAppId();
        var ret = TSLobsterAPI.AppFunction.GetSystemConfigs(workid, appid, 'SubMchId');
        // TSLobsterAPI.Utils.TrackLog('SubMchId',ret);
        if (!ret.data.config.SubMchId) {
            throw '系统参数SubMchId没有配置';
        }
        //获取收银台页面
        var param = {
            SubMchId: ret.data.config.SubMchId,
            BusinessOrderNo: model.Id.toString(),
            OrderType: 2,
            PayType: 1,
            PayScene: 1,
            Price: model.Fee * 100,
            Body: '处方缴费',
            Memo: '备注' //可以不填
        };
        // TSLobsterAPI.Utils.TrackLog('param',param);
        var res = TSLobsterAPI.AppFunction.CallOutService(appid, 'pay.pay.preorder', param);
        TSLobsterAPI.Utils.TrackLog('payredirecturl', res);
        var url = res.data.payredirecturl; //收银台界面，支付成功后回调订阅服务register.register.toregister，然后会调用his接口
        return { result: true, url: url };
    };
    //缴费支付回调
    tsselfpayService.prototype.balancepaysuccess = function (backpayobj) {
        // let Id;
        // let TranSerNo = '';
        // let OrderNo = '';
        var PayMode = 1;
        var PayTime = TSLobsterAPI.Utils.getCurrTime("yyyy-MM-dd HH:mm:ss");
        var info = this.dao.getPresPaymentInfo(parseInt(backpayobj.BusinessOrderNo));
        var param = {
            Repno: info["Repnos"],
            TranserNo: backpayobj.TransactionId,
            PayTime: PayTime,
            PayMode: PayMode,
            payAmt: info["Fee"]
        };
        var ret = this.hisinterface.balancePatPresDetail(param);
        if (ret.result) {
            this.dao.updatePresPayed(parseInt(backpayobj.BusinessOrderNo), backpayobj.TransactionId, backpayobj.OrderNo, PayMode);
        }
        else {
            throw '调用his接口失败';
        }
        return { result: true };
    };
    return tsselfpayService;
}());

//author:kakake
var tsselfpayController = /** @class */ (function () {
    function tsselfpayController() {
        this.data = {
            dbConn: TSLobsterAPI.Program.GetDbConn(),
            WorkId: 1,
            AppId: 1
        };
        this.data.WorkId = TSLobsterAPI.Program.GetWorkId();
        this.hisinterface = new tshisinterfaceController(1);
        this.service = new tsselfpayService(this.data.WorkId);
        this.dao = new tsselfpayDao(this.data.WorkId);
    }
    //获取未缴费处方
    tsselfpayController.prototype.getUnpaidPatPres = function () {
        var CardType = TSLobsterAPI.Program.RequestQuery("CardType");
        var CardNum = TSLobsterAPI.Program.RequestQuery("CardNum");
        return this.hisinterface.getPatPresUnPay(parseInt(CardType), CardNum);
    };
    //获取处方明细
    tsselfpayController.prototype.getPresDetail = function () {
        var repno = TSLobsterAPI.Program.RequestQuery("repno");
        return this.hisinterface.getPatPresDetail(repno);
    };
    //获取已缴费处方
    tsselfpayController.prototype.getpaidPatPres = function () {
        var CardType = TSLobsterAPI.Program.RequestQuery("CardType");
        var CardNum = TSLobsterAPI.Program.RequestQuery("CardNum");
        var StarDate = TSLobsterAPI.Program.RequestQuery("StarDate");
        var EndDate = TSLobsterAPI.Program.RequestQuery("EndDate");
        return this.hisinterface.getPatPresFinished(parseInt(CardType), CardNum, StarDate, EndDate);
    };
    //缴费处方
    tsselfpayController.prototype.balance = function () {
        var model = JSON.parse(TSLobsterAPI.Program.RequestBody());
        return this.service.balance(model);
    };
    //获取处方缴费记录
    tsselfpayController.prototype.getpresrecordlist = function () {
        var pageInfo = {
            key: "Id desc",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };
        var PatientId = TSLobsterAPI.Program.RequestQuery('PatientId', '0');
        var userId = TSLobsterAPI.Program.GetMPUserId();
        var workid = TSLobsterAPI.Program.GetWorkId();
        var strsql = "select * from WPMT_PresPayment where workid=@workid and MpUserId=@userId";
        if (parseInt(PatientId) > 0) {
            strsql += " and PatientId=@PatientId";
        }
        var dbparams = {
            workid: workid,
            userId: userId,
            PatientId: PatientId
        };
        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo);
        return { list: data, count: pageInfo.totalRecord };
    };
    //获取缴费处方费用明细
    tsselfpayController.prototype.getpresfeelist = function () {
        var id = TSLobsterAPI.Program.RequestQuery("id", 0);
        var strsql = "select PatName,PayTime,TranserNo,Fee from WPMT_PresPayment where id=@id";
        var head = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, { id: id });
        var strsql2 = "select ItemName,UnitPrice,Quantity,Unit from WPMT_FeeDetail where RecordId=@id ";
        var list = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql2, { id: id });
        return { head: head, list: list };
    };
    /**
     * 自助缴费记录
     * 提供给Web后台查询
     */
    tsselfpayController.prototype.getpresrecordlist_web = function () {
        var pageInfo = {
            key: "Id desc",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };
        var sdate = TSLobsterAPI.Program.RequestQuery('sdate');
        var edate = TSLobsterAPI.Program.RequestQuery('edate');
        var status = TSLobsterAPI.Program.RequestQuery('status'); //0全部 1待支付 2已缴费 3已退款 4已取消
        var keyword = TSLobsterAPI.Program.RequestQuery('keyword') || '';
        var workid = TSLobsterAPI.Program.GetWorkId();
        var dbparams = {
            workid: workid,
            sdate: sdate,
            edate: edate,
            status: status,
            keyword: "%" + keyword + "%"
        };
        //PayStatus 1 未支付 2已支付
        //OrderState 订单状态: 1待付款、2已付款、5已取消、6退款中、7退款完成、
        var strsql = "select * from WPMT_PresPayment  where WorkId=@workid and PatName like @keyword and PayTime BETWEEN @sdate and @edate";
        if (parseInt(status) > 0) {
            if (parseInt(status) == 1) {
                strsql += ' and OrderState=1';
            }
            else if (parseInt(status) == 2) {
                strsql += ' and OrderState=2';
            }
            else if (parseInt(status) == 3) {
                strsql += ' and OrderState=7';
            }
            else if (parseInt(status) == 4) {
                strsql += ' and OrderState=5';
            }
        }
        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo);
        return { list: data, count: pageInfo.totalRecord };
    };
    /**
     * 自助缴费记录
     * 费用明细
     */
    tsselfpayController.prototype.getpresfeelist_web = function () {
        var id = TSLobsterAPI.Program.RequestQuery("id", 0);
        var strsql2 = "select ItemName,UnitPrice,Quantity,Unit from WPMT_FeeDetail where RecordId=@id ";
        var list = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql2, { id: id });
        return { list: list };
    };
    return tsselfpayController;
}());

